package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;

public class KeeperInterceptBall extends State implements Constants {

	private static KeeperInterceptBall instance = null;
	
	public static KeeperInterceptBall state(){
		if(instance == null){
			instance = new KeeperInterceptBall();
			instance.name = "Intercept ball";
		}
		return instance;
	}
	
	
	@Override
	public void enter(BaseEntity owner) {
		GoalKeeper keeper = (GoalKeeper) owner;
		keeper.AP().pursuitOn(keeper.team.pitch.ball);
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		GoalKeeper keeper = (GoalKeeper) owner;
		
		if(keeper.tooFarFromGoalMouth() && !keeper.isClosestPlayerOnPitchToBall()){
			keeper.FSM().changeState(KeeperReturnHome.state());
			return;
		}
		
		if(keeper.isBallWithinRange()){
			keeper.team.pitch.ball.trap(keeper);
			keeper.FSM().changeState(KeeperPutBallBackInPlay.state());
			return;
		}
		
		
	}

	@Override
	public void exit(BaseEntity owner) {
		GoalKeeper keeper = (GoalKeeper) owner;
		keeper.AP().pursuitOff();
	
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}
	

}
